import 'package:flutter/cupertino.dart';
import '../../resources/kq_theme_colors.dart';

import '../../config/kq_global.dart';
import '../../resources/images.dart';
import '../../theme/kq_theme_manager.dart';
import '../button/kq_ink_well.dart';
import 'kq_marquee.dart';

/// 通知公告栏
///
/// @author 周卓
///
/// 左边图标 + 中间跑马灯 + 右边关闭按钮
class KqNoticeView extends StatefulWidget {
  /// 水平间距
  final double? horizontalPadding;

  /// 垂直间距
  final double? verticalPadding;

  /// 图标尺寸
  final double? iconSize;

  /// 跑马灯高度，当使用[richContent]时，传这个控制文本高度
  final double? textHeight;

  /// 跑马灯内容-纯文本，[content] 和 [richContent] 至少传一个
  final String? content;

  /// 跑马灯内容-富文本，[content] 和 [richContent] 至少传一个
  final RichText? richContent;

  /// 背景颜色
  final Color? backgroundColor;

  /// 文字样式，仅在纯文本有效
  final TextStyle? textStyle;

  /// 关闭回调
  final VoidCallback? onClose;

  /// 通知图标
  final Widget? noticeIcon;

  /// 关闭图标
  final Widget? closeIcon;

  /// 是否显示关闭图表
  final bool closable;

  const KqNoticeView(
      {Key? key,
      this.content,
      this.richContent,
      this.textStyle,
      this.iconSize,
      this.backgroundColor,
      this.horizontalPadding,
      this.verticalPadding,
      this.noticeIcon,
      this.textHeight,
      this.closable = true,
      this.closeIcon,
      this.onClose})
      : assert(
            (content == null && richContent != null) ||
                (content != null && richContent == null),
            'content 和 richContent 至少传一个，且不能都传'),
        super(key: key);

  @override
  State<KqNoticeView> createState() => _KqNoticeViewState();
}

class _KqNoticeViewState extends State<KqNoticeView> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: widget.backgroundColor ??
          KqThemeManager.getCommonConfig().mainLightColor!.withOpacity(0.05),
      padding: EdgeInsets.symmetric(
          vertical: widget.verticalPadding ?? 8,
          horizontal: widget.horizontalPadding ?? 16),
      child: Row(
        children: [
          //通知图标
          widget.noticeIcon ??
              Image.asset(
                Images.commonIcNoticeInform,
                width: widget.iconSize ?? 26,
                height: widget.iconSize ?? 26,
                color: KqThemeManager.getCommonConfig().mainLightColor,
                package: KqGlobal.packageName,
              ),
          const SizedBox(
            width: 12,
          ),
          //通知内容，跑马灯
          Expanded(
              child: SizedBox(
            height: widget.textHeight ?? widget.iconSize ?? 26,
            child: KqMarquee(
              text: widget.content,
              richText: widget.richContent,
              blankSpace: 150,
              style: widget.textStyle ??
                  TextStyle(
                      fontSize: 16,
                      color: KqThemeManager.getCommonConfig().mainLightColor ??
                          KqThemeColors.textLightBlue),
            ),
          )),
          const SizedBox(
            width: 12,
          ),
          //关闭按钮
          Visibility(
            visible: widget.closable,
            child: KqInkWell(
              radius: 100,
              child: widget.closeIcon ??
                  Image.asset(
                    Images.commonIcCloseBlue,
                    width: widget.iconSize ?? 26,
                    height: widget.iconSize ?? 26,
                    color: KqThemeManager.getCommonConfig().mainLightColor,
                    package: KqGlobal.packageName,
                  ),
              onTap: () {
                widget.onClose?.call();
              },
            ),
          )
        ],
      ),
    );
  }
}
